from configparser import ConfigParser
from prompt_toolkit import prompt
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.history import InMemoryHistory


def write_config_to_ini(lutoken, luid, lsleep, lcangid, ini_path='config.ini'):
    """
    将用户输入写入 config.ini 文件。
    """
    config = ConfigParser()
    config.read(ini_path)

    config.set('DEFAULT', 'utoken', lutoken or config.get('DEFAULT', 'utoken'))
    config.set('DEFAULT', 'uid', luid or config.get('DEFAULT', 'uid'))
    config.set('DEFAULT', 'sleep', lsleep or config.get('DEFAULT', 'sleep'))
    config.set('DEFAULT', 'cangid', lcangid or config.get('DEFAULT', 'cangid'))

    with open(ini_path, 'w') as f:
        config.write(f)

    with open(ini_path, 'r') as f:
        print(f.read())


def main():
    history = InMemoryHistory()

    utoken = prompt('请输入utoken：', auto_suggest=AutoSuggestFromHistory(), history=history)
    uid = prompt('请输入uid：', auto_suggest=AutoSuggestFromHistory(), history=history)
    sleep = prompt('请输入休眠时间（秒）：', auto_suggest=AutoSuggestFromHistory(), history=history)
    cangid = prompt('请输入仓库ID：', auto_suggest=AutoSuggestFromHistory(), history=history)

    write_config_to_ini(utoken, uid, sleep, cangid)


if __name__ == '__main__':
    main()
